/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package de.hwrberlin.remo.model.impl;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;

import de.hwrberlin.remo.model.Fact;
import de.hwrberlin.remo.model.KnowledgeBase;
import de.hwrberlin.remo.model.KnowledgeBaseElement;
import de.hwrberlin.remo.model.RemoModelPackage;
import de.hwrberlin.remo.model.Rule;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Knowledge Base</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link de.hwrberlin.remo.model.impl.KnowledgeBaseImpl#getOwnedElements <em>Owned Elements</em>}</li>
 *   <li>{@link de.hwrberlin.remo.model.impl.KnowledgeBaseImpl#getNextID <em>Next ID</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class KnowledgeBaseImpl extends RemoObjectImpl implements KnowledgeBase {
	/**
	 * The cached value of the '{@link #getOwnedElements() <em>Owned Elements</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getOwnedElements()
	 * @generated
	 * @ordered
	 */
	protected EList<KnowledgeBaseElement> ownedElements;

	/**
	 * The default value of the '{@link #getNextID() <em>Next ID</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getNextID()
	 * @generated
	 * @ordered
	 */
	protected static final int NEXT_ID_EDEFAULT = 0;

	/**
	 * The cached value of the '{@link #getNextID() <em>Next ID</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getNextID()
	 * @generated
	 * @ordered
	 */
	protected int nextID = NEXT_ID_EDEFAULT;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected KnowledgeBaseImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return RemoModelPackage.Literals.KNOWLEDGE_BASE;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<KnowledgeBaseElement> getOwnedElements() {
		if (ownedElements == null) {
			ownedElements = new EObjectContainmentEList<KnowledgeBaseElement>(KnowledgeBaseElement.class, this, RemoModelPackage.KNOWLEDGE_BASE__OWNED_ELEMENTS);
		}
		return ownedElements;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public int getNextID() {
		return nextID;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setNextID(int newNextID) {
		int oldNextID = nextID;
		nextID = newNextID;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, RemoModelPackage.KNOWLEDGE_BASE__NEXT_ID, oldNextID, nextID));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public EList<Fact> getFacts() {
		BasicEList<Fact> factList = new BasicEList<Fact>();
		
		for(KnowledgeBaseElement kbElement : getOwnedElements()) {
			if(kbElement instanceof Fact) {
				factList.add((Fact) kbElement);
			}
		}
		
		return factList;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public EList<Rule> getRules() {
		BasicEList<Rule> ruleList = new BasicEList<Rule>();
		
		for(KnowledgeBaseElement kbElement : getOwnedElements()) {
			if(kbElement instanceof Rule) {
				ruleList.add((Rule) kbElement);
			}
		}
		
		return ruleList;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case RemoModelPackage.KNOWLEDGE_BASE__OWNED_ELEMENTS:
				return ((InternalEList<?>)getOwnedElements()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case RemoModelPackage.KNOWLEDGE_BASE__OWNED_ELEMENTS:
				return getOwnedElements();
			case RemoModelPackage.KNOWLEDGE_BASE__NEXT_ID:
				return getNextID();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case RemoModelPackage.KNOWLEDGE_BASE__OWNED_ELEMENTS:
				getOwnedElements().clear();
				getOwnedElements().addAll((Collection<? extends KnowledgeBaseElement>)newValue);
				return;
			case RemoModelPackage.KNOWLEDGE_BASE__NEXT_ID:
				setNextID((Integer)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case RemoModelPackage.KNOWLEDGE_BASE__OWNED_ELEMENTS:
				getOwnedElements().clear();
				return;
			case RemoModelPackage.KNOWLEDGE_BASE__NEXT_ID:
				setNextID(NEXT_ID_EDEFAULT);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case RemoModelPackage.KNOWLEDGE_BASE__OWNED_ELEMENTS:
				return ownedElements != null && !ownedElements.isEmpty();
			case RemoModelPackage.KNOWLEDGE_BASE__NEXT_ID:
				return nextID != NEXT_ID_EDEFAULT;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (nextID: ");
		result.append(nextID);
		result.append(')');
		return result.toString();
	}

} //KnowledgeBaseImpl
